package com.all.learning.guava.other;

import java.util.Collection;
import java.util.Map;

import org.junit.Test;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.MapDifference.ValueDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class GGmap {

	public static void main(String[] args) {
		// create 2
		Multimap<String, String> myMultimap = ArrayListMultimap.create();
		// Adding some key/value
		myMultimap.put("Fruits", "Bannana");
		myMultimap.put("Fruits", "Apple");
		myMultimap.put("Fruits", "Pear");
		myMultimap.put("Vegetables", "Carrot");
		for (Map.Entry<String, String> entry : myMultimap.entries()) {
			System.out.println(entry.getKey() +"===="+entry.getValue());
			
		}
	}

	@Test
	public void testMultimap() {
		// create 1
		ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
		builder.put("name", "value");

		ImmutableMap<String, String> map = builder.build();
		System.out.println(map);

		// create 2
		Multimap<String, String> myMultimap = ArrayListMultimap.create();
		// Adding some key/value
		myMultimap.put("Fruits", "Bannana");
		myMultimap.put("Fruits", "Apple");
		myMultimap.put("Fruits", "Pear");
		myMultimap.put("Vegetables", "Carrot");

		// Getting the size
		int size = myMultimap.size();
		System.out.println(size); // 4

		// Getting values
		Collection<String> fruits = myMultimap.get("Fruits");
		System.out.println(fruits); // [Bannana, Apple, Pear]

		// Removing a single value
		myMultimap.remove("Fruits", "Pear");
		System.out.println(myMultimap.get("Fruits")); // [Bannana, Pear]

		// Remove all values for a key
		myMultimap.removeAll("Fruits");
		System.out.println(myMultimap.get("Fruits")); // [] (Empty Collection!)
	}

	@Test
	public void testImmutableMap() {
		ImmutableMap<String, String> map1 = ImmutableMap.of("key1", "value3", "key2", "value2");
		ImmutableMap<String, String> map2 = ImmutableMap.of("key2", "value2", "key1", "value1");

		MapDifference<String, String> diff = Maps.difference(map1, map2);
		System.out.println(diff.areEqual());

		// key相同，value不同
		Map<String, ValueDifference<String>> ent = diff.entriesDiffering();
		System.out.println(ent);

		Map<String, String> left = diff.entriesOnlyOnLeft();
		System.out.println("不同部分" + left);

		Map<String, String> comm = diff.entriesInCommon();
		System.out.println("相同" + comm);
	}

	/**
	 * value 和它 key 一样也是不可重复
	 */
	@Test
	public void testBiMap() {
		BiMap<Integer, String> biMap = HashBiMap.create();
		biMap.put(1, "Hydrogen");
		biMap.put(2, "Helium");
		biMap.put(3, "Lithium");
		biMap.put(4, "a.log");
		biMap.forcePut(5, "a.log");//否则IllegalArgumentException
		System.out.println(biMap.get(5));// 打印a.log
		System.out.println(biMap.get(4));// null

		System.out.println(biMap.inverse().get("Helium"));
	}

}
